//
//  ShopManagementController.swift
//  iModern
//
//  Created by 张义帅 on 15/8/24.
//  Copyright (c) 2015年 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class ShopManagementController: FormViewController
{

    var contactProfile = NSMutableDictionary()

    var articleManager = AdvertisingManager()

    override func viewDidLoad()
    {
        super.viewDidLoad()

        super.viewDidLoad()
        self.title = "匠铺设置"
        self.initializeForm()
        self.initBackButton()
        let button = UIButton()
        button.frame = CGRectMake(0, 0, 50, 30)
        button.setTitle("保存", forState: UIControlState.Normal)
        button.setTitleColor(Theme.theme_color, forState: .Normal)
        button.addTarget(self, action: Selector("save"), forControlEvents: UIControlEvents.TouchUpInside)
        let showButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = showButton

    }

    func save()
    {
        
//        println(self.formValues()["cellphone"])
//        println(self.formValues()["shopNotice"])
        
        let cellphone = self.formValues()["cellphone"] as? String

        if (cellphone != nil && (cellphone!).utf8.count > 0)
        {
            self.contactProfile["cellphone"] = self.formValues()["cellphone"]

        }
        else
        {
            self.contactProfile["cellphone"]=appDelegate.contactManager.contact["cellphone"] as! String
        }
        
        let shopNotice = self.formValues()["shopNotice"] as? String
        if shopNotice == nil
        {
            self.alertWarningMessage("请填写匠铺公告")
            return
        }
        if (shopNotice != nil && (shopNotice!).utf8.count > 0)
        {
             self.contactProfile["shopNotice"] = self.formValues()["shopNotice"]
        }
        else
        {
            self.contactProfile["shopNotice"]=appDelegate.contactManager.contact["shopNotice"] as? String
        }
        
        
        let rs = appDelegate.contactManager .updateProfile(contactProfile as [NSObject:AnyObject])

        if rs == nil
        {
            self.alertNetworkConnectionException()
        }

        if rs!.hasErrors()
        {
            if rs?.errorCode == 3020
            {
                alertUserStatusException()
                NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                self.view.endEditing(true)
                navigationController?.popToRootViewControllerAnimated(true)
            }
            alertErrorMessage(rs!.errorMessage)
        }
        self.view.endEditing(true)
        appDelegate.contactManager.loadContact()
        NSNotificationCenter.defaultCenter().postNotificationName("Refresh Shop", object: nil)
        self.navigationController?.popToRootViewControllerAnimated(true)

    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()

    }

    func initializeForm()
    {
        
//        println(appDelegate.contactManager.contact)
        var form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        form = XLFormDescriptor(title: "")

        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)

        let shopName = appDelegate.contactManager.contact["shopName"] as! String

        row = XLFormRowDescriptor(tag: "shopName", rowType: XLFormRowDescriptorTypeText, title: "匠铺名称：")
        row.cellConfigAtConfigure["textField.placeholder"] = shopName
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)

        let address = appDelegate.contactManager.contact["address"] as! String


        row = XLFormRowDescriptor(tag: "address", rowType: XLFormRowDescriptorTypeText, title: "匠铺地址：")
        row.cellConfigAtConfigure["textField.placeholder"] = address
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)
        
//        var shopNotice = appDelegate.contactManager.contact["shopNotice"] as? String
//        if shopNotice != nil
//        {
//            row.cellConfigAtConfigure["textField.placeholder"] = shopNotice
//            
//        }
//        row = XLFormRowDescriptor(tag: "shopNotice", rowType: XLFormRowDescriptorTypeText, title: "匠铺公告：")
//        row.required = true
//        section.addFormRow(row)

        
        
        let shopNotice = appDelegate.contactManager.contact["shopNotice"] as? String
        
        row = XLFormRowDescriptor(tag: "shopNotice", rowType: XLFormRowDescriptorTypeText, title: "匠铺公告：")
        if shopNotice != nil
        {
        row.cellConfigAtConfigure["textField.placeholder"] = shopNotice

        }
        row.required = true
        section.addFormRow(row)

        
        

        let cellphone = appDelegate.contactManager.contact["cellphone"] as! String

        row = XLFormRowDescriptor(tag: "cellphone", rowType: XLFormRowDescriptorTypePhone, title: "客服电话：")
        row.cellConfigAtConfigure["textField.placeholder"] = cellphone
        row.required = true
        row.disabled = NSNumber(bool: true)
        section.addFormRow(row)


       

        self.form = form
    }
}
